package com.itheima.system.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.QueryPageBean;
import com.itheima.system.dao.DeviceManageMapper;
import com.itheima.system.dao.DeviceMapper;
import com.itheima.system.dao.OrderMapper;
import com.itheima.system.pojo.Device;
import com.itheima.system.pojo.Order;
import com.itheima.system.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import java.util.Map;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DeviceManageMapper deviceManageMapper;
    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Device> findAll() {
        List<Device> deviceList = deviceMapper.selectAll();
        return deviceList;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Device findById(Long id) {
        return deviceMapper.selectByPrimaryKey(id);
    }

    /**
     * 条件查询
     * @param name
     * @return
     */
    @Override
    public List<Device> findList(String name) {
        Example example = new Example(Device.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("name","%"+name+"%");
        List<Device> deviceList = deviceMapper.selectByExample(example);
        return deviceList;
    }

    /**
     * 条件+分页查询
     * @param
     * @return
     */
    @Override
    public Page<Device> findPage(Integer page,Integer size,Map<String,Object> searchMap) {
        PageHelper.startPage(page,size);
        Example example = new Example(Device.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            //设备名称
            if (searchMap.get("name")!=null&& !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            //设备类型
            if (searchMap.get("type")!=null&&!"".equals(searchMap.get("type"))){
                criteria.andLike("type","%"+searchMap.get("type")+"%");
            }
            //品牌
            if (searchMap.get("brand")!=null&&!"".equals(searchMap.get("brand"))){
                criteria.andLike("brand","%"+searchMap.get("brand")+"%");
            }
            //型号
            if (searchMap.get("model")!=null&&!"".equals(searchMap.get("model"))){
                criteria.andEqualTo("model","%"+searchMap.get("model")+"%");
            }
            //应用场景
            if (searchMap.get("scenarios")!=null&&!"".equals(searchMap.get("scenarios"))){
                criteria.andEqualTo("scenarios","%"+searchMap.get("scenarios")+"%");
            }
            //价格
            if (searchMap.get("price")!=null&&!"".equals(searchMap.get("price"))){
                String price = (String) searchMap.get("price");
                // 100-200 100-*  *-200
                String[] split = price.split("-");
                if (split[1].equals("*")){
                    criteria.andLessThanOrEqualTo("price",split[1]);
                }
                if (split[0].equals("*")){
                    criteria.andGreaterThanOrEqualTo("price",split[0]);
                }
                if (split.length==2){
                    criteria.andBetween("price",split[0],split[1]);
                }
            }
        }
        List<Device> deviceList = deviceMapper.selectByExample(example);
        return (Page<Device>) deviceList;
    }

    /**
     * 添加
     * @param order
     */
    @Override
    public void add(Order order) {
        orderMapper.insert(order);
/*        Long did = order.getDid();
        String stringDid = did.toString();
        Integer intDid = Integer.valueOf(stringDid);
        DeviceManage deviceManage = new DeviceManage(null,intDid,Integer.valueOf(order.getNum()), "0", "0", "美国");
        deviceManageMapper.insertSelective(deviceManage);*/
    }
}
